(function(window) {
	var vm;
	var devSN = "";
	var userName = "";
	
	function managerPagePulldownRefresh(isPulldownRefresh) {
		var self = this;
		isPulldownRefresh = isPulldownRefresh === undefined ? true : isPulldownRefresh;
		setTimeout(function() {
			isPulldownRefresh && self.endPullDownToRefresh();
		}, 2000);
	}
	
	function getSystemInfo(){
		v3DevMonitor.getSystemInfo(devSN, function(code, data) {
			if(code === 0) {
				if(data){
					vm.systemInfo.devName = data.devName;
					vm.systemInfo.devSN = devSN;
					vm.systemInfo.devOnlineTime = formatTime(Number(data.devOnlineTime));
					vm.systemInfo.devSoftVersion = data.devSoftVersion + '/' +data.devHardVersion;
					vm.systemInfo.diskAvaliable = (data.diskAvaliable / (1024 * 1024)).toFixed(2) + ' MB';
					vm.systemInfo.cpuRatio = data.cpuRatio;
					vm.systemInfo.memoryRatio = data.memoryRatio;
					vm.systemInfo.devMode = data.devMode;
					vm.systemInfo.status = data.status;
					vm.systemInfo.statusText = data.status === 1 ? '已连接': '未连接';
				}
			} else {
				
			}
		});
	}
	
	// 格式化秒数到时间格式
	function formatTime(t) {
		// 计算
		var day = 0,
			h = 0,
			i = 0,
			s = parseInt(t);
		if(s > 60) {
			i = parseInt(s / 60);
			s = parseInt(s % 60);
			if(i > 60) {
				h = parseInt(i / 60);
				i = parseInt(i % 60);
			}
		}
		// 补零
		var zero = function(v) {
			return(v >> 0) < 10 ? "0" + v : v;
		};
		if(h >= 24) {
			day = parseInt(h / 24);
			return day + vm.$t("day");
		}
		return [zero(h), zero(i), zero(s)].join(":");
	};

	function mounted() {
		vm = this;

		$.plusReady(function() {
			devSN = $.util.getDevSN();
			userName = dataManager.getUserName();
			managerPagePulldownRefresh(false);
			getSystemInfo();
			$('.mui-scroll-wrapper').scroll({
				indicators: true, //是否显示滚动条
				bounce: false,
				deceleration: 0.0005
			});

			$("#manager-scroll").pullToRefresh({
				down: {
					callback: managerPagePulldownRefresh
				}
			});
		});
	}
	
	window.manager = {
		mounted: mounted,
		data: {
			systemInfo: {
				devName: "",
				devSN: "",
				devOnlineTime: "",
				devSoftVersion: "",
				diskAvaliable: "",
				cpuRatio: 0,
				memoryRatio: 0,
				devMode: "",
				status: 0,
				statusText: ''
			}
		},
		methods: {
			//monitorPagePulldownRefresh:monitorPagePulldownRefresh
		}
	};
})(window);